package bingosoft.metro.datasync.uuap.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bingosoft.components.base.helper.ObjectHelper;
import bingosoft.components.base.service.ResultEntity;
import bingosoft.metro.datasync.base.service.FullSyncService;
import bingosoft.metro.datasync.uuap.UuapClientFactory;
import bingosoft.metro.datasync.uuap.model.SyncAppMenuModel;
import bingosoft.metro.datasync.uuap.client.form.MenuDataForm;
import bingosoft.metro.datasync.uuap.client.service.DataClientService;
import leap.core.AppContext;
import leap.orm.model.Model;

/**
 * @author Feily
 * @功能描述 菜单全量同步数据服务类
 * @创建时间 2018-07-31
 */
public class MenuSyncService extends FullSyncService {
	
	/**
	 * 构建函数
	 */
	public MenuSyncService() {
		this.setLogClass(this.getClass());
		
		this.tenantCode = AppContext.config().getProperty("uuap.tenantCode");
		
		this.deleteSyncDataSql = this.getSqlContent("uuap-menu-deleteSyncData");
		this.saveTargetDataSql = this.getSqlContent("uuap-menu-saveTargetData");
		this.updateTargetDataSql = this.getSqlContent("uuap-menu-updateTargetData");
		this.deleteTargetDataSql = this.getSqlContent("uuap-menu-deleteTargetData");
	}

	@Override
	public List<Model> getSyncDataList() {
		this.logMethodCalled("getSyncDataList");
		
		List<Model> list = new ArrayList<Model>();
		
		DataClientService service  = UuapClientFactory.getDataClientService();
		ResultEntity<List<MenuDataForm>> resultEntity = service.queryMenus();
		if(!resultEntity.isSuccess()){
			throw new RuntimeException(resultEntity.getMessage());
		}
		
		// 构建数据
		List<MenuDataForm> menus = resultEntity.getBusinessObject();
		for (MenuDataForm menuDataForm : menus) {
			SyncAppMenuModel menu = new SyncAppMenuModel();
			menuDataForm.copyTo(menu);
			menu.setTenantCode(this.tenantCode);
			
			menu.setSyncId(ObjectHelper.getUUID());
			menu.setSyncTime(new Date());
			menu.setIsSync(true);
			
			list.add(menu);
		}
		return list;
	}
}
